<template>
  <roleManageView
    :isLoading="isLoading"
    :clients="clients"
    :roletableData="roletableData"
    :total="total"
    :pageSize="pageSize"
    :currentPage="currentPage"
    :addRoledialog="addRoledialog"
    :addRoleform="addRoleform"
    :appListSelect="appListSelect"
    :editRoledialog="editRoledialog"
    :editRoleform="editRoleform"
    :levelListSelect="levelListSelect"
    @findRole="findRole"
    @changePage="changePage"
    @sizeChange="sizeChange"
    @roleAdd="roleAdd"
    @addRoleClose="addRoleClose"
    @addRoleCancel="addRoleCancel"
    @addRoleKeep="addRoleKeep"
    @editRole="editRole"
    @editRoleClose="editRoleClose"
    @editRoleCancel="editRoleCancel"
    @editRoleKeep="editRoleKeep"
    @delRole="delRole"
    ref="roleManage"
  >

  </roleManageView>
</template>

<script>
  import roleManageView from './roleManageView.vue';

  export default {
    components: {
      roleManageView
    },
    name: "roleManageLogic",
    props: {},
    filters: {},
    computed: {},
    watch: {},
    data() {
      return {
        isLoading: false, // 加载状态
        clients: [], // 应用列表
        roletableData: [], // 角色列表
        tableDataSum: [], // 角色列表过滤前
        total: 0, // 总条数
        pageSize: 10, // 显示条数
        currentPage: 1, // 当前页
        addRoledialog: false, // 显示
        addRoleform: {
          name: "",
          desciption: "",
          clientId: "",
          // path: "",
          level: ""
        },
        editRoleform: {
          name: "",
          desciption: "",
          clientId: "",
          // path: "",
          level: ""
        },
        appListSelect: [], // app应用
        editRoledialog: false,
        selectRow: {}, // 选中行
        levelListSelect: [] // 角色等级列表
      }
    },
    created() {
      this.getList();
      this.getClientList();
      this.getRoleLevel();
    },
    mounted() {
    },
    activated() {
      this.getRoleLevel();
    },
    beforeRouteLeave(to, from, next) {
      console.log("Leave")
      // ....
      next()
    },
    beforeRouteEnter(to, from, next) {
      console.log("Enter")
      // ....
      next()
    },
    beforeRouteUpdate(to, from, next) {
      console.log("Update")
      // ....
      next()
    },
    methods: {
      // 获取角色列表
      getList(obj) {
        this.isLoading = true;
        _https.post.getRoleList(obj).then(res => {
          if (res.data.code == 0) {
            let data = res.data.data;
            let newData = [...data];
            let rolelevel = this.levelListSelect;
            let newArr = newData.map(item => {
              for (let i = 0; i < rolelevel.length; i++) {
                if (item.level == rolelevel[i].datavalue) {
                  item.levelname = rolelevel[i].datacode;
                  break;
                }
              }
              return item;
            });

            this.tableDataSum = newArr;
            let currentPage = this.currentPage;
            if (data.length % this.pageSize == 0 && currentPage != 1) {
              currentPage = Math.ceil(data.length / this.pageSize);
              this.currentPage = currentPage;
            }
            this.setDataList(currentPage);
            this.total = data.length;
            this.isLoading = false;
          } else {
            this.$message({type: 'error', message: res.data.message});
          }
        })
      },
      // 获取应用列表,查询的时候用的
      getClientList() {
        _https.post.getAppList().then(res => {
          if (res.data.code == 0) {
            this.appListSelect = res.data.data;
            let newArr = [...res.data.data];
            newArr.unshift({
              id: "",
              name: "全部"
            });
            this.clients = newArr;
          } else {
            this.$message({type: 'error', message: res.data.message});
          }
        })
      },
      // TODO
      // 获取角色等级列表
      getRoleLevel() {
        let obj = {
          datatype: "role_level"
        };
        _https.post.getDict(obj).then(res => {
          if (res.data.code == 0) {
            this.levelListSelect = res.data.data;
          }
        })
      },
      /**
       *  查询角色
       *  @param clientsValue 选择的应用id
       *  @param roleSeach 搜索框的内容
       */
      findRole(clientsValue, roleSeach) {
        this.currentPage = 1;
        this.isLoading = true;
        let obj = {
          id: clientsValue,
          name: roleSeach
        };
        if (clientsValue == "" && roleSeach == "") {
          this.getList();
        } else {
          this.getList(obj);
        }
      },
      /**
       * 改变页数的回调
       * @param page 返回当前页数
       */
      changePage(page) {
        this.currentPage = page;
        this.setDataList(page);
      },
      /**
       * 改变每页条数的回调
       * @param num 每页条数
       */
      sizeChange(num) {
        this.pageSize = num;
        this.getList();
      },
      /**
       * 将返回的数据进行分页操作
       * @param currentPage {Number}
       */
      setDataList(currentPage) {
        let current = currentPage * this.pageSize;
        let currentStart = current - this.pageSize;
        let currentEnd = current;
        this.roletableData = this.tableDataSum.slice(currentStart, currentEnd);
      },
      /**
       * 新增角色
       */
      roleAdd() {
        this.addRoledialog = true;
      },
      /**
       * 关闭弹窗的回调
       */
      addRoleClose() {
        this.addRoledialog = false;
        this.$refs.roleManage.$refs.addRoleform.resetFields();
      },
      /**
       * 取消添加角色
       */
      addRoleCancel() {
        this.addRoledialog = false;
      },
      /**
       * 保存添加角色
       */
      addRoleKeep() {
        let addRoleform = this.addRoleform;
        this.$refs.roleManage.$refs.addRoleform.validate((valid) => {
          if (!valid) {
            return;
          } else {
            let newObj = {
              desciption: addRoleform.desciption,
              name: addRoleform.name,
              // path: addRoleform.path,
              show: 0,
              clientId: addRoleform.clientId,
              level: addRoleform.level
            }
            _https.post.getSaveRole(newObj).then(res => {
              if (res.data.code == 0) {
                this.$message({type: 'success', message: res.data.message});
                this.addRoledialog = false;
                this.getList();
                this.resetNoRules();
              } else {
                this.$message({type: 'error', message: res.data.message});
              }
            })
          }
        })
      },
      /**
       * 编辑角色
       * @param index 行下标
       * @param row 行信息
       */
      editRole(index, row) {
        console.log("row", row);
        this.editRoleform = {
          name: row.name,
          desciption: row.desciption,
          clientId: row.clientId,
          // path: row.path,
          level: row.level
        };
        this.selectRow = row;
        this.editRoledialog = true;
      },
      // 关闭编辑窗口的回调
      editRoleClose() {
        this.editRoledialog = false;
      },
      // 取消编辑角色
      editRoleCancel() {
        this.editRoledialog = false;
      },
      // 保存编辑角色
      editRoleKeep() {
        let editRoleform = this.editRoleform;
        this.$refs.roleManage.$refs.editRoleform.validate((valid) => {
          if (!valid) {
            return;
          } else {
            let newObj = {
              id: this.selectRow.id,
              desciption: editRoleform.desciption,
              name: editRoleform.name,
              // path: editRoleform.path,
              show: this.selectRow.show,
              clientId: editRoleform.clientId,
              level: editRoleform.level
            }
            _https.post.getUpdateRole(newObj).then(res => {
              if (res.data.code == 0) {
                this.$message({type: 'success', message: res.data.message});
                this.getList();
                this.editRoledialog = false;
              } else {
                this.$message({type: 'error', message: res.data.message});
              }
            })
          }
        })
      },
      /**
       * 删除角色
       * @param index 行下标
       * @param row 行信息
       */
      delRole(index, row) {
        let newObj = {
          id: row.id
        };
        this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _https.post.getDeleteRole(newObj).then(res => {
            if (res.data.code == 0) {
              this.$message({type: 'success', message: res.data.message});
              this.getList();
            } else {
              this.$message({type: 'error', message: res.data.message});
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      resetNoRules() {
        this.addRoleform = {desciption: ""}
      }
    },
    destroyed() {

    }
  }
</script>

<style scoped lang="less">

</style>
